/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.fscan.filter;

import com.hyts.fscan.base.FileScannerCenter;
import com.hyts.fscan.domain.Directory;
import com.hyts.fscan.validate.DirectoryRuleValidate;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FilenameFilter;

/**
 * @ClassName: BaseLoadFileFilter
 * @Description: 基础操作加载文件名称过滤器服务接口实现类
 * @author: LiBo/Alex Lee
 * @date: 2017年9月27日 上午1:27:10
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class BaseLoadFileFilter implements FilenameFilter {
	
	/**
	 * @fieldName: directory
	 * @fieldType: Directory
	 * @Description: 目录操作服务
	 */
	private Directory directory;
	
	/**
	 * @Title:BaseLoadFileFilter
	 * @Description:基础服务信息文件加载过滤器
	 */
	public BaseLoadFileFilter(Directory directory) {
		this.directory = directory;
	}

	/* (non Javadoc)
	 * @Title: accept
	 * @Description: 扫描进行过滤操作
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
	 */
	@Override
	public boolean accept(File dir, String fileName) {
		//校验测试非空名称文件
		boolean flag = true;
		//校验限制参数
		if(FileScannerCenter.scanGetFileCurrentNum.get() == this.directory.getMaxScanNum()){
			return false;
		}
		//二层校验:校验文件是否合法
		File file = new File(dir,fileName);
		if(!file.exists() || !file.isFile()){
			return false;
		}
		//if(StringUtils.isNotEmpty(fileName))
		//{
			//判断是校验目录操作
			if(DirectoryRuleValidate.valiateDirectory(directory))
			{
				//校验过滤扩展名
				flag = DirectoryRuleValidate.isContainsExtName(extractFileExtName(fileName), this.directory.getExcludeFileExtName());
				//校验过滤文件名
				if(!flag)
				{
					flag = DirectoryRuleValidate.isContainsName(extractFileName(fileName), this.directory.getExcludeFileName());
				}
			}
		//}
		//校验是否为符合需求
		if(!flag){
			FileScannerCenter.scanGetFileCurrentNum.incrementAndGet();
		}
		return !flag;
	}

	/**
	 * @Title: extractFileExtName
	 * @Description: 抽取扩展名
	 * @param fileName
	 * @return
	 * @return: String
	 */
	private static String extractFileExtName(String fileName){
		 int extNameIndex = StringUtils.lastIndexOf(fileName, ".");
		 return StringUtils.substring(fileName, extNameIndex+1).toLowerCase(); 
	}
	
	/**
	 * @Title: extractFileName
	 * @Description: 抽取文件名
	 * @param fileName
	 * @return
	 * @return: String
	 */
	private static String extractFileName(String fileName){
		 int extNameIndex = StringUtils.lastIndexOf(fileName, ".");
		 return StringUtils.substring(fileName,0,extNameIndex).toLowerCase(); 
	}
	
}
